//
//  BIDEmailOutboxViewController.m
//  shangwuting
//
//  Created by mal on 13-12-26.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDEmailOutboxViewController.h"
#import "BIDDetailEmailFromOutboxViewController.h"
#import "BIDEmailDetailViewController.h"

//发件箱列表
static NSString *sentEmailListURL = @"emailAndroid!gainsendEmail.action";
//删除
static NSString *deleteEmailURL = @"emailAndroid!dropSendedEmail.action";

@interface BIDEmailOutboxViewController ()<BIDBaseListViewDelegate>

@end

@implementation BIDEmailOutboxViewController
@synthesize bRegister;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"发件箱";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //detailViewController = [[BIDDetailEmailFromOutboxViewController alloc] initWithNibName:@"BIDDetailEmailFromOutboxViewController" bundle:nil];
    
    bRegister = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.dataSourceArr removeAllObjects];
    self.curPage = 0;
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], sentEmailListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//删除列表中的一个邮件
- (void)deleteEmailAtIndexPath:(NSIndexPath*)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteEmailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", dic[@"id"]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [self.myTableView beginUpdates];
                [self.dataSourceArr removeObjectAtIndex:indexPath.row];
                [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [self.myTableView endUpdates];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    NSUInteger row = [indexPath row];
    NSDictionary *dictionary = self.dataSourceArr[row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.imageView.image = [UIImage imageNamed:@"email_list_flag.png"];
    cell.textLabel.text = [dictionary objectForKey:@"title"];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.detailTextLabel.text = [dictionary objectForKey:@"time"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13.0f];
    [cell setBackgroundColor:[UIColor clearColor]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    NSDictionary *dictionary = self.dataSourceArr[row];
    //BIDDetailEmailFromOutboxViewController *detailViewController = [[BIDDetailEmailFromOutboxViewController alloc] initWithNibName:@"BIDDetailEmailFromOutboxViewController" bundle:nil];
    BIDEmailDetailViewController *vc = [[BIDEmailDetailViewController alloc] initWithNibName:@"BIDEmailDetailViewController" bundle:nil];
    vc.emailID = dictionary[@"id"];
    //detailViewController.emailId = [dictionary objectForKey:@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self deleteEmailAtIndexPath:indexPath];
    }
}

@end
